package org.jeecg.modules.qyxx.service.impl;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.hibernate.validator.constraints.NotBlank;
import org.jeecg.modules.qyxx.entity.BgFpHzJdJzdSxygysjxsHb;
import org.jeecg.modules.qyxx.entity.BgFpHzNdSygys;
import org.jeecg.modules.qyxx.entity.BgFpHzNdXyjxs;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.entity.resp.GetSxyFpSjFxVo;
import org.jeecg.modules.qyxx.mapper.BgFpHzJdJzdSxygysjxsHbMapper;
import org.jeecg.modules.qyxx.service.IBgFpHzJdJzdSxygysjxsHbService;
import org.jeecg.modules.qyxx.service.IBgFpHzNdSygysService;
import org.jeecg.modules.qyxx.service.IBgFpHzNdXyjxsService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 上下游供应商和经销商汇总数据环比
 * @Author: jeecg-boot
 * @Date: 2022-08-09
 * @Version: V1.0
 */
@Service
public class BgFpHzJdJzdSxygysjxsHbServiceImpl extends MppServiceImpl<BgFpHzJdJzdSxygysjxsHbMapper, BgFpHzJdJzdSxygysjxsHb> implements IBgFpHzJdJzdSxygysjxsHbService {
    @Autowired
    private IBgFpHzNdSygysService bgFpHzNdSygysService;
    @Autowired
    private IBgFpHzNdXyjxsService bgFpHzNdXyjxsService;

    @Override
    public Object getSxyFpSjFx(BgNsrjbxxReq req) {
        String djxh = req.getDjxh();
        List<String> tjnds = DateUtils.getInstance().nearlyThreeYears(req.getKssj(), req.getJssj());
        // Map<String, Object> map = new LinkedHashMap<>();
        List<GetSxyFpSjFxVo> arrayList = new ArrayList<>();
        for (String tjnd : tjnds) {
            BigDecimal gysFpjeCount = BigDecimal.ZERO;
            BigDecimal jxsFpjeCount = BigDecimal.ZERO;
            GetSxyFpSjFxVo vo = new GetSxyFpSjFxVo();
            vo.setDjxh(djxh);
            vo.setTjnd(tjnd);
            List<BgFpHzJdJzdSxygysjxsHb> list = baseMapper.getSxyFpSjFx(djxh, tjnd);
            for (BgFpHzJdJzdSxygysjxsHb hb : list) {
                BigDecimal gysFpje = hb.getGysFpje();
                BigDecimal jxsFpje = hb.getJxsFpje();
                if (gysFpje != null) {
                    gysFpje = gysFpje.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    gysFpjeCount = gysFpjeCount.add(gysFpje);
                }
                if (jxsFpje != null) {
                    jxsFpje = jxsFpje.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    jxsFpjeCount = jxsFpjeCount.add(jxsFpje);
                }
                String jdxh = hb.getJdxh();
                switch (jdxh) {
                    case "1":
                        if (gysFpje != null) {
                            vo.setOnejdGysFpje(gysFpje);
                        }
                        if (jxsFpje != null) {
                            vo.setOnejdJxsFpje(jxsFpje);
                        }
                        break;
                    case "2":
                        if (gysFpje != null) {
                            vo.setTwojdGysFpje(gysFpje);
                        }
                        if (jxsFpje != null) {
                            vo.setTwojdJxsFpje(jxsFpje);
                        }
                        break;
                    case "3":
                        if (gysFpje != null) {
                            vo.setThreejdGysFpje(gysFpje);
                        }
                        if (jxsFpje != null) {
                            vo.setThreejdJxsFpje(jxsFpje);
                        }
                        break;
                    case "4":
                        if (gysFpje != null) {
                            vo.setFourjdGysFpje(gysFpje);
                        }
                        if (jxsFpje != null) {
                            vo.setFourjdJxsFpje(jxsFpje);
                        }
                        break;
                }
            }
            BgFpHzNdSygys sygys = bgFpHzNdSygysService.getByDjxhAndTjnd(djxh, tjnd);
            if (sygys != null) {
                if (sygys.getGysFpje() != null) {
                    vo.setGysFpje(sygys.getGysFpje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }
                //if (sygys.getGysFpsl() != null) {
                vo.setGysFpsl(sygys.getGysFpsl());
                /*} else {
                    vo.setGysFpsl(BigDecimal.ZERO);
                }*/
            } else {
                vo.setGysFpje(gysFpjeCount);
                // vo.setGysFpsl(BigDecimal.ZERO);
            }
            BgFpHzNdXyjxs xyjxs = bgFpHzNdXyjxsService.getByDjxhAndTjnd(djxh, tjnd);
            if (xyjxs != null) {
                if (xyjxs.getJxsFpje() != null) {
                    vo.setJxsFpje(xyjxs.getJxsFpje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }
                //if (xyjxs.getJxsFpsl() != null) {
                vo.setJxsFpsl(xyjxs.getJxsFpsl());
                /*} else {
                    vo.setJxsFpsl(BigDecimal.ZERO);
                }*/
            } else {
                vo.setJxsFpje(jxsFpjeCount);
                // vo.setJxsFpsl(BigDecimal.ZERO);
            }
            // map.put(tjnd, vo);
            arrayList.add(vo);
        }
        return arrayList;
    }
}
